<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 数组去重
    let arr=[1,1,22,22,33,33]

    // Set数据结构
    function handleArr(arr) {
      return [...new Set(arr)]
    }

    console.log(handleArr(arr));

    // 对象属性的唯一性
    function handleArr1(arr) {
      let obj={},
      newArr=[]
      for (let i = 0; i < arr.length; i++) {
        if(!obj[arr[i]]){
          obj[arr[i]]=1
          newArr.push(arr[i])
        }
      }
      return newArr
    }
    console.log(handleArr1(arr));

    // 对象数组去重
    let objArr=[
      {name:'zs'},
      {name:'zs'},
      {name:'zs'},
      {name:'lisi'},
      {name:'lisi'},
      {name:'lisi'},
    ]

    function handleArr2(arr) {  
      let obj={},
      newArr=[]

      arr.map(item=>{
        if(!obj[item.name]){
          obj[item.name]=1
          newArr.push(item)
        }
      })
      return newArr
    }
    console.log(handleArr2(objArr));

    // 数组快排
    let sArr=[33,21,2,4,20]
    function handleArr3(arr) {
      if(arr<=1){
        return arr
      }
      // 获取中间项的值并从数组中取出
      let midIndex=Math.floor(arr.length/2)
      let midVal=arr.splice(midIndex,1)

      // 准备两个数组，左数组放比中间项小的，右数组放比中间项大的
      let left=[],
        right=[]
      // 遍历比较
      for (let i = 0; i < arr.length; i++) {
        arr[i]<midVal?left.push(arr[i]):right.push(arr[i])
      }
      // 递归调用
      return handleArr3(left).concat(midVal,handleArr3(right))
    }
    console.log('快排',handleArr3(sArr));

    let sArr1=[33,21,2,4,20]
    sArr1.sort((a,b)=>{
      return a-b
    })
    console.log('sort快排',sArr1);
  </script>
</body>
</html>